<?php


namespace RedMVC\Auth\Adapter;
use RedMVC;

/**
 * Db class
 *  
 * @category    RedMVC
 * @package     Auth
 * @subpackage  Adapter 
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 */
class Db implements IAdapter{

    protected $_db;
    protected $_sql;
    protected $_loginResult;

    public function __construct(array $sql){
        $this->_sql = $sql;        
    }
    
    public function setDb($db){
        $this->_db = $db;
    }
    
    protected function _getDb(){
        if(null === $this->_db){
            $this->_db = RedMVC\Registry::get('RedMVC_Db');
        }
        return $this->_db;
    }
    
    public function login(){
        $db     = $this->_getDb();
        $result = call_user_func_array(array($db, 'fetch'), $this->_sql);
        if(count($result) === 1){
            $this->_loginResult = $result[0];
            return true;
        }
        return false;
    }
    
    public function getLoginResult(){
        return $this->_loginResult;
    }

}